package com.hunttown.mes.impl;

import com.hunttown.mes.rpc.domain.TransportPurchaseDetailDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseDetailDTOQuery;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseDetailRpcService;
import com.hunttown.mes.service.TransportPurchaseDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportPurchaseDetailConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:06
 */
@Service
public class TransportPurchaseDetailRpcServiceImpl implements TransportPurchaseDetailRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseDetailRpcServiceImpl.class);

    private final TransportPurchaseDetailService transportPurchaseDetailService;

    @Autowired
    public TransportPurchaseDetailRpcServiceImpl(TransportPurchaseDetailService transportPurchaseDetailService) {
        this.transportPurchaseDetailService = transportPurchaseDetailService;
    }

    @Override
    public TransportPurchaseDetailDTO insert(TransportPurchaseDetailDTO transportPurchaseDetailDTO) {
        try {
            return convert(transportPurchaseDetailService.insert(convert(transportPurchaseDetailDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail create failed.", e);
            throw new RuntimeException("TransportPurchaseDetail create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseDetailDTO transportPurchaseDetailDTO) {
        try {
            return transportPurchaseDetailService.updateInfoById(convert(transportPurchaseDetailDTO));
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail update by id failed.", e);
            throw new RuntimeException("TransportPurchaseDetail update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseDetailDTOQuery transportPurchaseDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDetailDTOQuery.getW_id() != null && transportPurchaseDetailDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseDetailDTOQuery.getW_id());
            }

            if (transportPurchaseDetailDTOQuery.getW_cRepairId() != null) {
                query.put("w_cRepairId", transportPurchaseDetailDTOQuery.getW_cRepairId());
            }

            if (transportPurchaseDetailDTOQuery.getW_partId() != null) {
                query.put("w_partId", transportPurchaseDetailDTOQuery.getW_partId());
            }

            if (transportPurchaseDetailDTOQuery.getW_purchaseType() != null) {
                query.put("w_purchaseType", transportPurchaseDetailDTOQuery.getW_purchaseType());
            }

            if (transportPurchaseDetailDTOQuery.getW_opType() != null) {
                query.put("w_opType", transportPurchaseDetailDTOQuery.getW_opType());
            }

            if (transportPurchaseDetailDTOQuery.getW_relationId() != null) {
                query.put("w_relationId", transportPurchaseDetailDTOQuery.getW_relationId());
            }

            if (transportPurchaseDetailDTOQuery.getW_tableId() != null && transportPurchaseDetailDTOQuery.getW_tableId() > 0) {
                query.put("w_tableId", transportPurchaseDetailDTOQuery.getW_tableId());
            }

            if (transportPurchaseDetailDTOQuery.getW_recordId() != null && transportPurchaseDetailDTOQuery.getW_recordId() > 0) {
                query.put("w_recordId", transportPurchaseDetailDTOQuery.getW_recordId());
            }

            if (transportPurchaseDetailDTOQuery.getW_purId() != null && transportPurchaseDetailDTOQuery.getW_purId() > 0) {
                query.put("w_purId", transportPurchaseDetailDTOQuery.getW_purId());
            }

            if (transportPurchaseDetailDTOQuery.getIds() != null && !transportPurchaseDetailDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseDetailDTOQuery.getIds());
            }

            if (transportPurchaseDetailDTOQuery.getBxId() != null) {
                query.put("bxId", transportPurchaseDetailDTOQuery.getBxId());
            }

            if (transportPurchaseDetailDTOQuery.getStatus() != null) {
                query.put("status", transportPurchaseDetailDTOQuery.getStatus());
            }

            if (transportPurchaseDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDetailDTOQuery.getStopFlag());
            }

            if (transportPurchaseDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDetailDTOQuery.getDeleteFlag());
            }

            return transportPurchaseDetailService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail update by query failed.", e);
            throw new RuntimeException("TransportPurchaseDetail update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDetailDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseDetailService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail select by id failed.", e);
            throw new RuntimeException("TransportPurchaseDetail select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDetailDTO selectObjByQuery(TransportPurchaseDetailDTOQuery transportPurchaseDetailDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDetailDTOQuery.getPartId() != null && transportPurchaseDetailDTOQuery.getPartId() > 0) {
                query.put("partId", transportPurchaseDetailDTOQuery.getPartId());
            }

            if (transportPurchaseDetailDTOQuery.getOpType() != null) {
                query.put("opType", transportPurchaseDetailDTOQuery.getOpType());
            }

            if (transportPurchaseDetailDTOQuery.getrPurchaseType() != null) {
                query.put("rPurchaseType", transportPurchaseDetailDTOQuery.getrPurchaseType());
            }

            if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getOrderBy())) {
                query.put("orderBy", transportPurchaseDetailDTOQuery.getOrderBy());
            }

            if (transportPurchaseDetailDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDetailDTOQuery.getStopFlag());
            }

            if (transportPurchaseDetailDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDetailDTOQuery.getDeleteFlag());
            }

            return convert(transportPurchaseDetailService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail select obj by query failed.", e);
            throw new RuntimeException("TransportPurchaseDetail select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseDetailDTO> getListForPage(TransportPurchaseDetailDTOQuery transportPurchaseDetailDTOQuery) {
        Query query = Query.page(transportPurchaseDetailDTOQuery.getPn(), transportPurchaseDetailDTOQuery.getPageSize());

        if (transportPurchaseDetailDTOQuery.getId() != null && transportPurchaseDetailDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseDetailDTOQuery.getId());
        }

        if (transportPurchaseDetailDTOQuery.getIds() != null && !transportPurchaseDetailDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseDetailDTOQuery.getIds());
        }

        if (transportPurchaseDetailDTOQuery.getEntId() != null && transportPurchaseDetailDTOQuery.getEntId() > 0) {
            query.put("entId", transportPurchaseDetailDTOQuery.getEntId());
        }

        if (transportPurchaseDetailDTOQuery.getrPurchaseType() != null) {
            query.put("rPurchaseType", transportPurchaseDetailDTOQuery.getrPurchaseType());
        }

        if (transportPurchaseDetailDTOQuery.getrCompanyId() != null && transportPurchaseDetailDTOQuery.getrCompanyId() != -9999) {
            query.put("rCompanyId", transportPurchaseDetailDTOQuery.getrCompanyId());
        }

        if (transportPurchaseDetailDTOQuery.getrPurchaser() != null && transportPurchaseDetailDTOQuery.getrPurchaser() != -9999) {
            query.put("rPurchaser", transportPurchaseDetailDTOQuery.getrPurchaser());
        }

        if (transportPurchaseDetailDTOQuery.getTableId() != null) {
            query.put("tableId", transportPurchaseDetailDTOQuery.getTableId());
        }

        if (transportPurchaseDetailDTOQuery.getRecordId() != null) {
            query.put("recordId", transportPurchaseDetailDTOQuery.getRecordId());
        }

        if (transportPurchaseDetailDTOQuery.getPurId() != null) {
            query.put("purId", transportPurchaseDetailDTOQuery.getPurId());
        }

        if (transportPurchaseDetailDTOQuery.getProId() != null && transportPurchaseDetailDTOQuery.getProId() > 0) {
            query.put("proId", transportPurchaseDetailDTOQuery.getProId());
        }

        if (transportPurchaseDetailDTOQuery.getPartId() != null && transportPurchaseDetailDTOQuery.getPartId() > 0) {
            query.put("partId", transportPurchaseDetailDTOQuery.getPartId());
        }

        if (transportPurchaseDetailDTOQuery.getPartIds() != null) {
            query.put("partIds", transportPurchaseDetailDTOQuery.getPartIds());
        }

        if (transportPurchaseDetailDTOQuery.getcCarId() != null && transportPurchaseDetailDTOQuery.getcCarId() > 0) {
            query.put("cCarId", transportPurchaseDetailDTOQuery.getcCarId());
        }

        if (transportPurchaseDetailDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseDetailDTOQuery.getPurchaseType());
        }

        if (transportPurchaseDetailDTOQuery.getcDriverId() != null) {
            query.put("cDriverId", transportPurchaseDetailDTOQuery.getcDriverId());
        }

        if (transportPurchaseDetailDTOQuery.getcUserId() != null && transportPurchaseDetailDTOQuery.getcUserId() > 0) {
            query.put("cUserId", transportPurchaseDetailDTOQuery.getcUserId());
        }

        if (transportPurchaseDetailDTOQuery.getsAdminId() != null) {
            query.put("sAdminId", transportPurchaseDetailDTOQuery.getsAdminId());
        }

        if (transportPurchaseDetailDTOQuery.getOpType() != null) {
            query.put("opType", transportPurchaseDetailDTOQuery.getOpType());
        }

        if (transportPurchaseDetailDTOQuery.getcRepairId() != null) {
            query.put("cRepairId", transportPurchaseDetailDTOQuery.getcRepairId());
        }

        if (transportPurchaseDetailDTOQuery.getBxId() != null) {
            query.put("bxId", transportPurchaseDetailDTOQuery.getBxId());
        }

        if (transportPurchaseDetailDTOQuery.getApplyId() != null) {
            query.put("applyId", transportPurchaseDetailDTOQuery.getApplyId());
        }

        if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getPurchasetNo())) {
            query.put("purchasetNo", transportPurchaseDetailDTOQuery.getPurchasetNo());
        }

        if (transportPurchaseDetailDTOQuery.getNoInStore() != null && transportPurchaseDetailDTOQuery.getNoInStore() == 1) {
            query.put("noInStore", transportPurchaseDetailDTOQuery.getNoInStore());
        }

        if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseDetailDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseDetailDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseDetailDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDetailDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseDetailDTOQuery.getOrderBy());
        }

        if (transportPurchaseDetailDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseDetailDTOQuery.getStopFlag());
        }

        if (transportPurchaseDetailDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseDetailDTOQuery.getDeleteFlag());
        }

        List<TransportPurchaseDetailDTO> list = convert(transportPurchaseDetailService.selectByQuery(query));
        Page<TransportPurchaseDetailDTO> page = Page.from(transportPurchaseDetailDTOQuery.getPn(), transportPurchaseDetailDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseDetailService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseDetailService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail delete failed.", e);
            throw new RuntimeException("TransportPurchaseDetail delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSumCount(TransportPurchaseDetailDTOQuery dtoQuery, String element) {
        try {
            Query query = Query.page(1, 5);

            if (dtoQuery.getPartId() != null) {
                query.put("partId", dtoQuery.getPartId());
            }

            return transportPurchaseDetailService.selectSumCount(query, element);
        } catch (Exception e) {
            logger.error("TransportPurchaseDetail select sum count failed.", e);
            throw new RuntimeException("TransportPurchaseDetail select sum count failed, Please order your code.", e);
        }
    }
}